<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
</head>

<body>
    <div id="app">
        <p>
            商品单价: <input type="number" v-model.number.lazy="price">
        </p>
        <p>
            购买数量: <input type="number" v-model.number.lazy="num">
        </p>
        <p>
            <!-- 小计: {{total}} -->
            小计: <money-handler :pay="total"></money-handler>
        </p>
    </div>
</body>

<script>

    // 父组件传入的数据如果子组件想修改的 -> 只能间接改?
    // 1. 将父组件传入的值作为子组件的初始值(data), 在子组件初始化时执行一次 
    // 2. 根据父组件传入的值得到新值=> 计算属性

    Vue.component("money-handler", {
        props: ["pay"],
        data() {
            return {
            }
        },
        computed: {
            money: function () {
                return new Intl.NumberFormat().format(this.pay);
            }
        },
        template: `<span>{{money}}</span>`
    })

    var app = new Vue({
        el: "#app",
        data: {
            price: 5898,
            num: 1,
        },
        computed: {
            total: function () {
                return (this.price * this.num).toFixed(2);
            }
        }


    })

</script>

</html>